/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: DriverBalanceMainController
 * Author:   李毅
 * Date:     2019/10/31 16:41
 * Description: 司机结算业务控制类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ymtms.settlement.controller;

import com.ymtms.settlement.pojo.DriverBalanceMain;
import com.ymtms.settlement.pojo.GoodsbillMaster;
import com.ymtms.settlement.pojo.Waybill;
import com.ymtms.settlement.service.DriverBalanceMainService;
import com.ymtms.settlement.service.DriverInformationService;
import com.ymtms.settlement.service.GoodsbillMasterService;
import com.ymtms.settlement.service.WaybillService;
import com.ymtms.settlement.vo.GridRequest;
import com.ymtms.settlement.vo.GridResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈司机结算业务控制类〉
 *
 * @author 李毅
 * @create 2019/10/31
 * @since 1.0.0
 */

@Log4j2
@RestController
@CrossOrigin
public class DriverBalanceMainController {

    @Autowired
    GoodsbillMasterService goodsbillMasterService;

    @Autowired
    DriverInformationService driverInformationService;

    @Autowired
    DriverBalanceMainService driverBalanceMainService;

    @Autowired
    WaybillService waybillService;

    //实现结算业务
    //首先，查出货运单状态为已结账的数据
    @RequestMapping("/getGoodsbillMaster")
    public GridResult getGoods(GridRequest gridRequest){
        GridResult result = new GridResult();
        String state = "已结账";
        try {
            List<GoodsbillMaster> goodsbills = goodsbillMasterService.findGoodsbillByState(state,gridRequest);
            result.setCount(goodsbillMasterService.findCount(state));
            result.setCode(0);
            result.setData(goodsbills);
            result.setMsg("查询成功");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    //展示在页面上并添加结算按钮进行结算操作
    @RequestMapping("/doSettlement")
    public GridResult doSettlement(Integer gmid, DriverBalanceMain driverBalanceMain){
        GridResult result = new GridResult();
        try {
            BigDecimal payedMoney = driverBalanceMain.getPayedMoney();  //已付金额
            if ( driverBalanceMain.getNeedPayment().equals(payedMoney)){
                driverBalanceMain.setBalanceType("已结算");
            }else{
                result.setCode(500);
                result.setMsg("金额不正确，结算失败！");
                return result;
            }
            //先根据gmid去查对应的运输单编号，再根据运输单编号来判断是否存在运输结算信息，有则修改，没有则插入
            Waybill waybill = waybillService.findWaybillBygmid(gmid);
            String contractCode = waybill.getContractCode();//运输单编号
            DriverBalanceMain balanceMainByCode = driverBalanceMainService.findDriverBalanceMainByCode(contractCode);
            driverBalanceMain.setDriverPkid(waybill.getDriverPkid());
            driverBalanceMain.setBalanceTime(new Date());   //结算时间
            driverBalanceMain.setInsurance(waybill.getInsurance()); //保险费
            BigDecimal dispatchFerviceFee = driverBalanceMain.getDispatchFerviceFee();  //配载服务费
            driverBalanceMain.setFlag(0);
            driverBalanceMain.setBackBillCode(waybill.getContractCode());
            //应付金额为承运费+定装费+加运费+保险费+配载服务费
            //计算余额:应付金额-已付金额
            driverBalanceMain.setBalance(driverBalanceMain.getNeedPayment().subtract(driverBalanceMain.getPayedMoney()));
            driverBalanceMainService.addDriverBalanceMain(driverBalanceMain);
            //改变货运单状态
            goodsbillMasterService.updateState(gmid);
            result.setCode(0);
            result.setMsg("结算成功");
        }
        catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    //显示历史结算信息
    @RequestMapping("/getDriverBalanceByState")
    public GridResult getDriverBalanceByState(GridRequest gridRequest,DriverBalanceMain driverBalanceMain){
        GridResult result = new GridResult();
        String backBillCode = driverBalanceMain.getBackBillCode();
        try {
            List<DriverBalanceMain> driverBalanceMains = driverBalanceMainService.findDriverBalanceMainByState(gridRequest,backBillCode);
            for (DriverBalanceMain balanceMain : driverBalanceMains) {
                driverBalanceMain.setDriver(driverInformationService.findDriver(balanceMain.getDriverPkid()));
            }
            result.setCode(0);
            result.setData(driverBalanceMains);
            result.setCount(driverBalanceMainService.findCount(backBillCode));
            result.setMsg("查询成功！");
        }catch (Exception e){
            result.setCode(500);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    //修改历史结算信息
    @RequestMapping("/updateBalance")
    public GridResult updateBalance(DriverBalanceMain driverBalanceMain){
        GridResult result = new GridResult();
        if (driverBalanceMain ==null){
            result.setMsg("请输入正确的信息！");
            result.setCode(500);
            return result;
        }
        try {
            driverBalanceMainService.updateDriverBalanceMain(driverBalanceMain);
            result.setCode(0);
            result.setMsg("修改成功！");
        }catch (Exception e){
            result.setMsg("修改失败！");
            result.setCode(500);
        }
        return result;
    }
    //删除历史结算信息
    @RequestMapping("deleteDriverBalance")
    public GridResult deleteDrivrBalance(Integer dbid){
        GridResult result = new GridResult();
        if (dbid == null){
            result.setMsg("请输入正确的信息！");
            result.setCode(500);
            return result;
        }
        try {
            driverBalanceMainService.deleteDriverBalance(dbid);
            result.setMsg("删除成功！");
            result.setCode(0);
        }catch (Exception e){
            result.setCode(500);
            result.setMsg("修改失败！");
        }
        return result;
    }
}